<?php
/**
 * Created by PhpStorm.
 * User: songdewei
 * Date: 2017/10/11
 * Time: 上午10:41
 */

namespace App\WeChat\WxApi;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

class WxNewsApi extends WxApi
{
    /**
     * @param $media_id
     * @return mixed
     * @throws \Exception
     */
    public static function getNews($media_id){
        $access_data  = json_encode(['media_id'=>$media_id]);
        $access_token = self::getAccessToken();
        $uri = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=$access_token";
        $request = new Request('POST', $uri, [], $access_data);
        $client  = new Client();
        $response = $client->send($request);
        if ($response->getStatusCode() == 200) {
            return $response->getBody();
        }
        return false;
    }

    /**
     * @param int $offset
     * @param int $count
     * @return mixed
     * @throws \Exception
     */
    public static function batchgetNews($offset=0, $count=20){
        $access_data = json_encode([
            'type'=>'news',
            'offset'=>$offset,
            'count'=>$count
        ]);
        $uri = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=".self::getAccessToken();
        $request = new Request('POST', $uri, [], $access_data);
        $client  = new Client();
        $response = $client->send($request);
        if ($response->getStatusCode() == 200) {
            return $response->getBody();
        }
        return false;
    }

    /**
     * @param $data
     * @return mixed
     * @desc
     * {
     * "articles": [{
     * "title": TITLE,
     * "thumb_media_id": THUMB_MEDIA_ID,
     * "author": AUTHOR,
     * "digest": DIGEST,
     * "show_cover_pic": SHOW_COVER_PIC(0 / 1),
     * "content": CONTENT,
     * "content_source_url": CONTENT_SOURCE_URL
     * },    //若新增的是多图文素材，则此处应还有几段articles结构
     * ]
     * }
     * @throws \Exception
     */
    public static function addNews($data){
        $uri = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=".self::getAccessToken();
        $request = new Request('POST', $uri, [], $data);
        $client  = new Client();
        $response = $client->send($request);
        if ($response->getStatusCode() == 200) {
            return $response->getBody();
        }
        return false;
    }

    /**
     * @param $media_id
     * @return mixed
     * @throws \Exception
     */
    public static function deleteNews($media_id){
        $access_data  = json_encode(['media_id'=>$media_id]);
        $uri = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=".self::getAccessToken();
        $request = new Request('POST', $uri, [], $access_data);
        $client  = new Client();
        $response = $client->send($request);
        if ($response->getStatusCode()) {
            return $response->getBody();
        }
        return false;
    }

    /**
     * @param $data
     * @return mixed
     * {
     * "media_id":MEDIA_ID,
     * "index":INDEX,
     * "articles": {
     * "title": TITLE,
     * "thumb_media_id": THUMB_MEDIA_ID,
     * "author": AUTHOR,
     * "digest": DIGEST,
     * "show_cover_pic": SHOW_COVER_PIC(0 / 1),
     * "content": CONTENT,
     * "content_source_url": CONTENT_SOURCE_URL
     * }
     * }
     * @throws \Exception
     */
    public static function updateNews($data){
        $uri = "https://api.weixin.qq.com/cgi-bin/material/update_news?access_token=".self::getAccessToken();
        $client = new Client();
        $response = $client->get($uri);
        if ($response->getStatusCode() == 200) {
            return $response->getBody();
        }
        return false;
    }
}
